package com.leave.mos.controller.simple;

import com.leave.mos.constant.TablenameConstant;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.CacheRequest;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @ClassName ComparisonQueryOperatorsController
 * @Description TODO
 * @Author lh
 * @Date 2021/7/12 16:50
 * @Version 1.0
 *
 * 逻辑查询运算  LogicalQueryOperators
 *
 **/
@RestController
@RequestMapping("logica")
public class LogicalQueryOperatorsController {

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 1.与运算
     * 2.$and执行的逻辑AND的阵列上操作的一个或多个表达式（例如<expression1>， <expression2>等），并且选择满足该文件 的所有阵列中的表达式。
     * 3.该$and运营商使用短路计算。
     * 4.如果第一个表达式（例如<expression1>）的计算结果为false，MongoDB 将不会计算剩余的表达式
     */
    @RequestMapping("and")
    public List<Map> and(){

        //{ "orderCode" : "order_10002", "$and" : [{ "orderCode" : { "$exists" : true}}]}
        Criteria c = new Criteria(TablenameConstant.goods_order.orderCode);
        c.is("order_10002");
        c.andOperator(Criteria.where(TablenameConstant.goods_order.orderCode).exists(true));
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    @RequestMapping("and1")
    public List<Map> and1(){

        //{ "orderCode" : "order_10002", "$and" : [{ "orderCode" : { "$exists" : true}}]}
        Criteria c = new Criteria(TablenameConstant.goods_order.orderCode);
        c.is("order_10002");
        Criteria c1 = Criteria.where(TablenameConstant.goods_order.orderCode).exists(true);
        Query query = new Query();
        query.addCriteria(c);
        query.addCriteria(c1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    @RequestMapping("and2")
    public List<Map> and2(){

        //{ "status" : { "$lt" : 2}, "$and" : [{ "boxUnitPrice" : 209}]}
        Criteria criteria = Criteria.where(TablenameConstant.goods_order.status).lt(2);
        criteria.andOperator(Criteria.where(TablenameConstant.goods_order.boxUnitPrice).is(209));

        Query query = new Query();
        query.addCriteria(criteria);//.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    /**
     *  $lte 运营商不同。仅{ $lte: 1.99 }返回字段存在且其值小于或等于 的文档 。price1.99.
     *  { $not: { $gt: 1.99 } } 该price字段的值小于或等于1.99 或 该price字段不存在.
     *
     *  注意：$not操作员仅影响其他操作员，不能独立检查字段和文档。因此，使用$not运算符进行逻辑析取，使用运算 $ne符直接测试字段的内容。
     * @return
     */
    @RequestMapping("not")
    public List<Map> not(){

        // Criteria criteria = Criteria.where(TablenameConstant.goods_order.status).lt(2).not();
        //not 要放到操作符的前面
        Criteria criteria = Criteria.where(TablenameConstant.goods_order.status).not().lt(2);

        Query query = new Query();
        query.addCriteria(criteria);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    static final String pa = Pattern.compile("^order.*").pattern();

    @RequestMapping("not1")
    public List<Map> not1(){
        //正则表达式
        Criteria criteria = Criteria.where(TablenameConstant.goods_order.orderCode).regex(pa).not();

        Query query = new Query();
        query.addCriteria(criteria);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    //$norNOR对一个或多个查询表达式的数组执行逻辑运算，并选择 数组中所有查询表达式失败的文档
    @RequestMapping("nor")
    public List<Map> nor(){
        // { "orderCode" : "order_100011", "$nor" : [{ "status" : 2}]}
        Criteria criteria = Criteria.where(TablenameConstant.goods_order.orderCode).is("order_100011")
                .norOperator(Criteria.where(TablenameConstant.goods_order.status).is(2));

        Query query = new Query();
        query.addCriteria(criteria);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    @RequestMapping("nor1")
    public List<Map> nor1(){
        // orderCode 存在 并且 等于 order_100011
        Criteria criteria = Criteria.where(TablenameConstant.goods_order.orderCode).is("order_100011")
                .norOperator(Criteria.where(TablenameConstant.goods_order.orderCode).exists(false));

        Query query = new Query();
        query.addCriteria(criteria);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    /**
     *  $or操作者执行的逻辑OR的阵列上操作的两个或更多 <expressions>和文件选择满足至少的一个<expressions>
     */
    @RequestMapping("or")
    public List<Map> or(){

        //{ "$or" : [{ "orderName" : "新订单3"}, { "status" : { "$gte" : 2}}]}
        Criteria criteria1 = new Criteria().orOperator(
                Criteria.where(TablenameConstant.goods_order.orderName).is("新订单3"),
                Criteria.where(TablenameConstant.goods_order.status).gte(2)
        );
        Query query = new Query();
        query.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }
}
